<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>OSSJK 2.0</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link rel="stylesheet" href="../../../static/layui/css/layui.css" media="all" />
<link rel="stylesheet" href="../../../static/css/public.css" media="all" />
<style>
.detailTable_text {
	font-weight: bold;
}
</style>
</head>
<body class="childrenBody">
	<form class="layui-form" lay-filter="formTest" id="form">
		<!-- 基本信息模块 -->
		<fieldset class="layui-elem-field layui-field-title site-title">
			<legend>
				<div>基础信息</div>
			</legend>
		</fieldset>
		<table class="layui-table mag0">
			<colgroup>
				<col width="15%">
				<col width="30%">
				<col width="15%">
				<col width="30%">
			</colgroup>
			<tbody class="detailTable">
				<tr>
					<td class="detailTable_text">学校名</td>
					<td colspan="3"><input type="text" class="layui-input " lay-verify="required" name="name"></td>
				</tr>
				<tr>
					<td class="detailTable_text">跟进人</td>
					<td><select name="eid" id="eid" class="searchVal">
							<option value="">请选择</option>
					</select></td>
					<td class="detailTable_text">合作级别</td>
					<td><select name="level" id="level" lay-verify="required">
							<option value="">请选择</option>
							<option value="1">深度合作</option>
							<option value="2">一般合作</option>
							<option value="3">意向合作</option>
							<option value="4">没合作</option>
					</select></td>
				</tr>
				<tr>
					<td class="detailTable_text">省市级</td>
					<td colspan="3"><div class="layui-form-item">
							<div id="area"></div>
						</div></td>
				</tr>
				<tr>
					<td class="detailTable_text">详细地址</td>
					<td colspan="3"><input type="text" name="addr" id="addr" lay-verify="required" autocomplete="off" class="layui-input"></td>
				</tr>
				<tr>
					<td class="detailTable_text">备注</td>
					<td colspan="3"><input type="text" class="layui-input" placeholder="" name="remarks"></input></td>
				</tr>
			</tbody>
		</table>
		<fieldset class="layui-elem-field layui-field-title site-title">
			<legend>
				<div>学校联系人</div>
			</legend>
		</fieldset>
		<table class="layui-table mag0" id="counterpart" lay-filter="counterpart">
			<thead>
				<tr>
					<th></th>
					<th>名称</th>
					<th>学院</th>
					<th>电话</th>
					<th>qq</th>
					<th>微信</th>
					<th>备注</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
			</tbody>
		</table>
		<div class="magt10 layui-right">
			<div class="layui-input-block">
				<button type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="submit">提交</button>
				<button type="button" class="layui-btn layui-btn-sm layui-btn-primary close-btn">取消</button>
			</div>
		</div>
	</form>
	<script type="text/javascript" src="../../../static/js/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="../../../static/layui/layui.js"></script>
	<script type="text/javascript" src="../../../static/js/index.js"></script>
	<script type="text/javascript" src="../../../static/custom/js/constant.js"></script>
	<script type="text/javascript" src="../../../static/custom/js/common.js"></script>
	<script type="text/javascript">
		layui.config({
			base : '../../../static/plugin/layui_extends/'
		}).extend({
			selectN : 'selectN'
		}).use([ 'selectN', 'form', 'table', 'laydate', 'layer' ], function() {
			var form = layui.form;
			var table = layui.table;
			var laydate = layui.laydate;
			var selectN = layui.selectN;
			var layer = parent.layer === undefined ? layui.layer : top.layer, $ = layui.jquery;
			var insertApi = "../../../advisory/school/insert";
			var toUpdateApi = "../../../advisory/school/toUpdate";
			var updateApi = "../../../advisory/school/update";
			var getEmployeeApi = "../../../getEmployee";
			var getAreaApi = "../../../getArea";
			var getRecordsHtml = "getRecords.html";
			var insertRecordsHtml = "insertRecords.html";
			var insertMethod = "post";
			var updateMethod = "put";
			var url = updateApi;
			var method = updateMethod;
			var areaData;
			var areaDefaults = [];

			//拼接子项目字段
			function counterpartHtml(index, obj) {
				if (!obj) {
					obj = {
						id : '',
						name : '',
						phone : '',
						qq : '',
						wechat : '',
						ername : '',
						eid : ''

					}
				}
				if (!index) {
					index = 1;
				}
				if (!obj.remarks) {
					obj.remarks = ''
				}
				var str = null;
				str += '<tr class="counterpart">                                                                                                   ';
				str += '	<td class="detailTable_text counterpart-text">学校联系人' + index + '</td>                                                                               ';
				str += '	<td><input type="text" class="layui-input cname" lay-verify="required"  name="cname" value="'+obj.name+'"><input type="hidden" class="layui-input id" lay-verify="required"  name="id" value="'+obj.id+'"></td>         ';
				str += '	<td><input type="text" class="layui-input cphone" lay-verify="required" name="faculty" value="'+obj.faculty+'"></td>         ';
				str += '	<td><input type="text" class="layui-input cphone" lay-verify="required" name="cphone" value="'+obj.phone+'"></td>         ';
				str += '	<td><input type="text" class="layui-input cqq" lay-verify="required"  name="cqq" value="'+obj.qq+'"></td>         ';
				str += '	<td><input type="text" class="layui-input cwechat" lay-verify="required"  name="cwechat" value="'+obj.wechat+'"></td>         ';
				str += '	<td><input type="text" class="layui-input cremarks"  name="cremarks"  value="'+obj.remarks+'"></td>         ';
				str += '	<td><a class="layui-btn layui-btn-xs listRecords-btn" style="display:none">跟进记录</a></td>';
				str += '</tr>';
				return str;
			}
			//查看跟进记录
			$("tbody").on("click", ".listRecords-btn", function() {
				var id = $(this).parent().parent().find(".id").val();
				openFullLayer("跟进记录", getRecordsHtml + "?cid=" + id);
			});
			function setText() {
				for (var i = 1; i <= $(".counterpart").length; i++) {
					$(".counterpart-text").eq(i - 1).text("学校联系人" + i);
				}

			}
			//回显表单
			var id = getUrlParam("id");
			var type = getUrlParam("type");

			loadAjax( getEmployeeApi, "get", null, function(response) {
				baseCallBack(response, function(response) {
					if (response.data && response.data.length) {
						for(var  v  of  response.data ){
							$("#eid").append('<option value="'+v.id+'">'+v.rname+'</option>');
							form.render("select"); 
						}
					}
					$("#form").append("<input type='hidden' name='id' value="+id+">");
					loadAjax( toUpdateApi + "/" + id, "get", null, function(response) {
						baseCallBack(response, function(response) {
							fomrVal(form, "formTest", response.data.school);
							$("#eid").val(response.data.employee.id);
							var length = 0;
							for ( var i in response.data.counterpart) {
								length++;
								$("#counterpart").children("tbody").append(counterpartHtml(parseInt(i) + 1, response.data.counterpart[i]));
							}
							form.render();
							//省市区联动
							loadAjax( getAreaApi, "get", null, function(response2) {
								baseCallBack(response2, function(response2) {
									areaData = response2.data;
									//无限级分类-所有配置
									selectN({
										//元素容器【必填】
										elem : '#area',
										data : areaData, //候选数据【必填】
										width : null,//设置了长度
										selected : response.data.areaIds,//默认值
										last : false, //为真只取最后一个值
										tips : '请选择', //空值项提示，可设置为数组['请选择省','请选择市','请选择县']
										name : 'areaIds',//input的name 不设置与选择器相同(去#.)
										delimiter : ',',//数据分隔符
										field : {//数据的键名
											idName : 'id',
											titleName : 'name',
											childName : 'children'
										}
									});
								});
							});
							form.render();
							$("input").attr("disabled", true);
							$("textarea").attr("disabled", true);
							$("select").attr("disabled", true);
							$("a").css("display", "none");
							$("button").css("display", "none");
							$(".listRecords-btn").css("display", "inline-block");
						});
					});
				});
			});
			//提交表单
			form.on("submit(submit)", function(data) {
				for (var i = 0; i < $(".cremarks").length; i++) {
					if ($(".cremarks").eq(i).val() == '') {
						$(".cremarks").eq(i).val(" ");
					}
				}
				if (type == 3) {
					url = insertApi;
					method = insertMethod;
				}
				loadAjax( url, method, $("#form").serialize(), function(response) {
					baseCallBack(response, parentReloadCallBack);
				});
				return false;
			})

		});
	</script>
</body>
</html>